﻿Public Class frm_create_sfdl

    Dim _new_sfdl_file As New SFDL_File

    Private Sub cmd_save_Click(sender As Object, e As EventArgs) Handles cmd_save.Click

        Dim _tools As New Tools
        Const SFDLFILEVERSION As Integer = 4

        Try

            If chk_sfdl_encrypt.Checked Then

                Dim _crypto As New Crypto.AES

                If String.IsNullOrEmpty(txt_sfdl_encrypt_password.Text) Then
                    Throw New ArgumentException("Du musst ein Passwort eingeben!")
                End If

                _new_sfdl_file.GetSetDescription = _crypto.EncryptString(txt_desc.Text, txt_sfdl_encrypt_password.Text)
                _new_sfdl_file.GetsetUploader = _crypto.EncryptString(txt_upper.Text, txt_sfdl_encrypt_password.Text)
                _new_sfdl_file.GetSetMaxDownloadThread = num_max_threads.Value
                _new_sfdl_file.GetSetFileVersion = SFDLFILEVERSION

                For Each _file In _new_sfdl_file.GetSetFileInfoList

                    _file.GetSetDirectoryPath = _crypto.EncryptString(_file.GetSetDirectoryPath, txt_sfdl_encrypt_password.Text)
                    _file.GetSetDirectoryRoot = _crypto.EncryptString(_file.GetSetDirectoryRoot, txt_sfdl_encrypt_password.Text)
                    _file.GetSetFileFullPath = _crypto.EncryptString(_file.GetSetFileFullPath, txt_sfdl_encrypt_password.Text)
                    _file.GetSetFileName = _crypto.EncryptString(_file.GetSetFileName, txt_sfdl_encrypt_password.Text)
                    _file.GetSetMD5FileHash = _crypto.EncryptString(_file.GetSetMD5FileHash, txt_sfdl_encrypt_password.Text)

                Next

                _new_sfdl_file.GetSetConnectionInfo.GetSetHost = _crypto.EncryptString(_new_sfdl_file.GetSetConnectionInfo.GetSetHost, txt_sfdl_encrypt_password.Text)
                _new_sfdl_file.GetSetConnectionInfo.GetSetName = _crypto.EncryptString(_new_sfdl_file.GetSetConnectionInfo.GetSetName, txt_sfdl_encrypt_password.Text)
                _new_sfdl_file.GetSetConnectionInfo.GetSetPassword = _crypto.EncryptString(_new_sfdl_file.GetSetConnectionInfo.GetSetPassword, txt_sfdl_encrypt_password.Text)
                _new_sfdl_file.GetSetConnectionInfo.GetSetPath = _crypto.EncryptString(_new_sfdl_file.GetSetConnectionInfo.GetSetPath, txt_sfdl_encrypt_password.Text)
                _new_sfdl_file.GetSetConnectionInfo.GetSetUsername = _crypto.EncryptString(_new_sfdl_file.GetSetConnectionInfo.GetSetUsername, txt_sfdl_encrypt_password.Text)

                _new_sfdl_file.GetSetEncryptet = True

            Else

                _new_sfdl_file.GetSetDescription = txt_desc.Text
                _new_sfdl_file.GetsetUploader = txt_upper.Text
                _new_sfdl_file.GetSetMaxDownloadThread = num_max_threads.Value
                _new_sfdl_file.GetSetFileVersion = SFDLFILEVERSION

                _new_sfdl_file.GetSetEncryptet = False

            End If

            _tools.XMLSerialize(_new_sfdl_file, txt_sfdl_file_path.Text)

            MessageBox.Show("SFDL Datei erfolgreich erstellt!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)

            _tools.ClearSFDLQueue()


            Me.Close()

        Catch ex As ArgumentException

            MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Warning)

        Catch ex As Exception
            MessageBox.Show("Fehler beim erstellen der Datei!", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

    Private Sub cmd_search_Click(sender As Object, e As EventArgs) Handles cmd_search.Click

        If Not SFD_File.ShowDialog = Windows.Forms.DialogResult.Cancel Then
            txt_sfdl_file_path.Text = SFD_File.FileName
            cmd_save.Enabled = True
        End If

    End Sub

    Private Sub frm_create_sfdl_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim _tools As New Tools
        Dim _settings_file_path As String
        Dim _settings As New Settings

        _settings_file_path = IO.Path.Combine(Application.StartupPath, "settings.xml")

        If IO.File.Exists(_settings_file_path) Then

            _settings = _tools.XMLDeSerialize(_settings, _settings_file_path)

            If Not String.IsNullOrEmpty(_settings.GetSetDefaultUpper) Then
                txt_upper.Text = _settings.GetSetDefaultUpper
            End If

            If Not String.IsNullOrEmpty(_settings.GetSetDefaultDescription) Then
                txt_desc.Text = _settings.GetSetDefaultDescription
            End If

            chk_sfdl_encrypt.Checked = _settings.GetSetDefaultEncryptEnabled

            If Not String.IsNullOrEmpty(_settings.GetSetDefaultEncryptPassword) Then
                txt_sfdl_encrypt_password.Text = _settings.GetSetDefaultEncryptPassword
            End If

            num_max_threads.Value = _settings.GetSetDefaultMaxThreads

        End If


    End Sub

    Private Sub chk_sfdl_encrypt_CheckedChanged(sender As Object, e As EventArgs) Handles chk_sfdl_encrypt.CheckedChanged

        If chk_sfdl_encrypt.Checked Then
            txt_sfdl_encrypt_password.Enabled = True
        Else
            txt_sfdl_encrypt_password.Enabled = False
        End If

    End Sub

    Public WriteOnly Property SFDLFile As SFDL_File
        Set(value As SFDL_File)
            _new_sfdl_file = value
        End Set
    End Property
End Class